package com.guguskins.back.controller;


import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.guguskins.back.common.annotation.Authorized;
import com.guguskins.back.entity.GuOrder;
import com.guguskins.back.entity.bo.PageBo;
import com.guguskins.back.entity.bo.TopUpBo;
import com.guguskins.back.entity.vo.PageVo;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.entity.vo.TopUpVo;
import com.guguskins.back.service.GuOrderService;
import com.guguskins.back.utils.JwtUtil;
import com.guguskins.back.utils.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

/**
 * <p>
 * 充值订单表 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-05-10
 */
@RestController
@RequestMapping("/order")
public class GuOrderController {

    @Autowired
    private GuOrderService orderService;

    /**
     * 校验
     */
    @PostMapping("/cal")
    @Authorized
    public ResultVo cal(@RequestHeader("token") String token,
                        @RequestBody TopUpBo topUpBo) {

        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        topUpBo.setUserId(userId);
        TopUpVo topUpVo = orderService.cal(topUpBo);
        return ResultVo.success(topUpVo);
    }


    /**
     * 充值
     */
    @PostMapping("topUp")
    @Authorized
    public void topUp(HttpServletResponse response,
                      @RequestHeader("token") String token,
                      @RequestBody TopUpBo topUpBo) throws IOException {

        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        topUpBo.setUserId(userId);
        Map<String, Object> result = orderService.topUp(topUpBo);
//        String content = orderService.topUp(topUpBo);

        BitMatrix bitMatrix = QRCodeUtil.createCode(result.get("content").toString());

        // 设置响应流信息
        response.setContentType("image/jpg");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Expose-Headers", "order-id");
        response.setHeader("order-id", result.get("orderId").toString());
        response.setDateHeader("Expires", 0);

        OutputStream stream = response.getOutputStream();

        //以流的形式输出到前端
        MatrixToImageWriter.writeToStream(bitMatrix, "jpg", stream);
    }

    /**
     * 分页
     */
    @PostMapping("/back/page")
    public ResultVo backPage(@RequestBody PageBo pageBo) {

        PageVo<GuOrder> pageVo = orderService.backPage(pageBo.getP(), pageBo.getSize(), pageBo.getStartTime(), pageBo.getEndTime(), pageBo.getOrderState(), pageBo.getSearchKey());

        return ResultVo.success(pageVo);
    }


    /**
     * 退款
     */
    @PostMapping("/back/refund")
    public ResultVo backRefund(@RequestBody GuOrder order) {

        orderService.backRefund(order.getOrderId());

        return ResultVo.success();
    }

}
